import gconf

class Settings:
    """A class to take care of the loading, exporting and storage of game 
    settings.
    
    Settings available:
      Setup:
        starting_level: 1 - 20
      
      Gameplay:
        random_colours: bool, use random colours for pieces
        shadow: bool, show a shadow where the piece will land

      Controls:
        left, right, down, rotate_clockwise, rotate_cclockwise,
        hard_drop, hold, pause

      Theme:
        The current directory that holds the theme tiles we want to use
    """

    controls = ["left", "right", "down", "rotate_clockwise",            
        "rotate_counterclockwise", "hard_drop", "hold", "pause"]


    def __init__(self, value=None):
        self.client = gconf.client_get_default()
        self.path = "/apps/schrenckii"

        self.client.add_dir(self.path, gconf.CLIENT_PRELOAD_NONE)


    def value(self, key):
        value = self.client.get(self.keypath(key))

        if value.type == gconf.VALUE_INT:
            return value.get_int()

        elif value.type == gconf.VALUE_STRING:
            return value.get_string()

        elif value.type == gconf.VALUE_BOOL:
            return value.get_bool()

        else:
            return value.to_string()


    def set(self, key, value):
        key = self.keypath(key)

        t = self.client.get(key).type

        if t == gconf.VALUE_INT:
            self.client.set_int(key, value)

        elif t == gconf.VALUE_STRING:
            self.client.set_string(key, value)

        elif t == gconf.VALUE_BOOL:
            self.client.set_bool(key, value)

        else:
            self.client.set_string(key, str(value))


    def keypath(self, key):
        return "%s/%s" % (self.path, key)


